# -*- coding: utf-8 -*-


"""
Adding a player screen.

It can change Game Phase from Phase.ADD_PLAYER to Phase.ADD_WORDS
Pressing the button "Add player" -> going to screen "Add_words".

I needs valid list of users (gamestate.players.players)
It will add Player object to list of users (gamestate.players.players)
"""

from kivy.app import App
from kivy.properties import StringProperty
from model.gamestate import *
from ui.hat_widget import HatWidget


class AddPlayerScreen(HatWidget):
    player_name = StringProperty()

    def __init__(self, game_state, presenter, **kwargs):
        """ Drawing screen which will be used for adding a player
        args:
            game_state: gamestate for the moment of launching
            presenter: current presenter

        """
        super(AddPlayerScreen, self).__init__(**kwargs)
        self.game_state = game_state
        self.presenter = presenter
        self.player_name =\
            u'{} {}'.format(_('Player'),
                            len(self.game_state.players.players)+1)
        self.text_input.select_all()

    def update_name(self, value):
        self.player_name = value

        name_is_acceptable = (self.player_name != '')
        for i in self.game_state.players.players:
            if i.name == self.player_name.strip():
                name_is_acceptable = 0

        if name_is_acceptable:
            self.to_add_button.enabled = True
        else:
            self.to_add_button.enabled = False

    def on_add_user_pressed(self):
        """ Called when button "Add user" is pressed". Adds user in gamestate.

            -puts in players the name of the user
            -puts him also to the current_added_player
        """
        tmp = Player(self.player_name.strip(), [])
        self.game_state.current_added_player = tmp
        self.presenter.change_phase(Phase.ADD_WORDS)

    def go_back(self):
        self.presenter.change_phase(Phase.START_GAME)


class AddPlayerApp(App):
    def build(self):
        return AddPlayerScreen()


if __name__ == '__main__':
    AddPlayerApp().run()
